package gzc.xiaonei.action;

import gzc.xiaonei.model.University;
import gzc.xiaonei.model.Users;
import gzc.xiaonei.model.Useruniversity;
import gzc.xiaonei.service.ICountryService;
import gzc.xiaonei.service.IProvinceService;
import gzc.xiaonei.service.IUniversityService;
import gzc.xiaonei.service.IUserService;
import gzc.xiaonei.service.IUseruniversityService;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/** 用户注册 */
@SuppressWarnings("unchecked")
public class registerAction extends ActionSupport {

	private String id;
	private String primaryschoolId;
	private String cityId;
	private String provinceId;
	private String juniorId;
	private String techschoolId;
	private String email;
	private String name;
	private String pwd;
	private String photo;
	private String sex;
	private String birth;
	private String tel;
	private String mobile;
	private String oicq;
	private String msn;
	private String website;
	private String techSchYear;
	private String juniorYear;
	private String primaryYear;
	private String interest;
	private String music;
	private String movie;
	private String game;
	private String cartoon;
	private String sport;
	private String book;
	private String assn;
	private String visited;
	private String doing;
	private String online;
	private String registerDate;
	private String loginDate;
	private String level;
	private String locked;
	// 增加一个接收大学id的字段
	private String universityId;
	// 增加一个用户类型字段
	private String userType;

	@Resource
	private ICountryService countryService;
	@Resource
	private IProvinceService provinceService;
	@Resource
	private IUniversityService universityService;
	@Resource
	private IUserService userService;
	@Resource
	private IUseruniversityService useruniversityService;

	/** 跳转到注册页面 */
	public String goRegUIAction() throws Exception {
		final Map<String, List<Object>> request = (Map<String, List<Object>>) ActionContext
				.getContext().get("request");
		// 准备数据，供用户选择大学使用
		// 国家
		request.put("countrylist",
				countryService.getResult("from Country", null));
		// 省和直辖市的信息(默认中国)
		request.put("provincelist", provinceService.getResult(
				"from Province where country.id=?", new Object[] { new Integer(
						1) }));
		// 大学名字(默认显示北京)
		request.put("unilist", universityService.getResult(
				"from University where province.id=? and country.id=?",
				new Object[] { new Integer(1), new Integer(1) }));

		return SUCCESS;
	}

	/** 真正处理用户注册请求 */
	@Override
	public String execute() throws Exception {
		// 取出信息，目的把用户保存
		Users user = new Users();
		user.setEmail(this.getEmail());
		user.setLoginDate(new java.util.Date());
		user.setName(this.getName());
		user.setPwd(this.getPwd());
		user.setRegisterDate(new java.util.Date());
		user.setSex(this.getSex());

		// 先查询出用户的大学对象.
		University university = (University) universityService.findById(
				University.class, Integer.valueOf(this.getUniversityId()));

		if (null != university) {
			// 创建该用户对应的用户大学记录
			Useruniversity useruniversity = new Useruniversity();
			useruniversity.setUsers(user);
			useruniversity.setUniversity(university);
			useruniversity.setUserType(this.getUserType());

			// 保存用户
			userService.save(user);
			// 保存用户对应的用户大学记录
			useruniversityService.save(useruniversity);

			return SUCCESS;
		}
		return ERROR;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getPrimaryschoolId() {
		return primaryschoolId;
	}

	public void setPrimaryschoolId(String primaryschoolId) {
		this.primaryschoolId = primaryschoolId;
	}

	public String getCityId() {
		return cityId;
	}

	public void setCityId(String cityId) {
		this.cityId = cityId;
	}

	public String getProvinceId() {
		return provinceId;
	}

	public void setProvinceId(String provinceId) {
		this.provinceId = provinceId;
	}

	public String getJuniorId() {
		return juniorId;
	}

	public void setJuniorId(String juniorId) {
		this.juniorId = juniorId;
	}

	public String getTechschoolId() {
		return techschoolId;
	}

	public void setTechschoolId(String techschoolId) {
		this.techschoolId = techschoolId;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getPhoto() {
		return photo;
	}

	public void setPhoto(String photo) {
		this.photo = photo;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getBirth() {
		return birth;
	}

	public void setBirth(String birth) {
		this.birth = birth;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getOicq() {
		return oicq;
	}

	public void setOicq(String oicq) {
		this.oicq = oicq;
	}

	public String getMsn() {
		return msn;
	}

	public void setMsn(String msn) {
		this.msn = msn;
	}

	public String getWebsite() {
		return website;
	}

	public void setWebsite(String website) {
		this.website = website;
	}

	public String getTechSchYear() {
		return techSchYear;
	}

	public void setTechSchYear(String techSchYear) {
		this.techSchYear = techSchYear;
	}

	public String getJuniorYear() {
		return juniorYear;
	}

	public void setJuniorYear(String juniorYear) {
		this.juniorYear = juniorYear;
	}

	public String getPrimaryYear() {
		return primaryYear;
	}

	public void setPrimaryYear(String primaryYear) {
		this.primaryYear = primaryYear;
	}

	public String getInterest() {
		return interest;
	}

	public void setInterest(String interest) {
		this.interest = interest;
	}

	public String getMusic() {
		return music;
	}

	public void setMusic(String music) {
		this.music = music;
	}

	public String getMovie() {
		return movie;
	}

	public void setMovie(String movie) {
		this.movie = movie;
	}

	public String getGame() {
		return game;
	}

	public void setGame(String game) {
		this.game = game;
	}

	public String getCartoon() {
		return cartoon;
	}

	public void setCartoon(String cartoon) {
		this.cartoon = cartoon;
	}

	public String getSport() {
		return sport;
	}

	public void setSport(String sport) {
		this.sport = sport;
	}

	public String getBook() {
		return book;
	}

	public void setBook(String book) {
		this.book = book;
	}

	public String getAssn() {
		return assn;
	}

	public void setAssn(String assn) {
		this.assn = assn;
	}

	public String getVisited() {
		return visited;
	}

	public void setVisited(String visited) {
		this.visited = visited;
	}

	public String getDoing() {
		return doing;
	}

	public void setDoing(String doing) {
		this.doing = doing;
	}

	public String getOnline() {
		return online;
	}

	public void setOnline(String online) {
		this.online = online;
	}

	public String getRegisterDate() {
		return registerDate;
	}

	public void setRegisterDate(String registerDate) {
		this.registerDate = registerDate;
	}

	public String getLoginDate() {
		return loginDate;
	}

	public void setLoginDate(String loginDate) {
		this.loginDate = loginDate;
	}

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public String getLocked() {
		return locked;
	}

	public void setLocked(String locked) {
		this.locked = locked;
	}

	public String getUniversityId() {
		return universityId;
	}

	public void setUniversityId(String universityId) {
		this.universityId = universityId;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public IUniversityService getUniversityService() {
		return universityService;
	}

	public void setUniversityService(IUniversityService universityService) {
		this.universityService = universityService;
	}

	public IProvinceService getProvinceService() {
		return provinceService;
	}

	public void setProvinceService(IProvinceService provinceService) {
		this.provinceService = provinceService;
	}

	public ICountryService getCountryService() {
		return countryService;
	}

	public void setCountryService(ICountryService countryService) {
		this.countryService = countryService;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public IUseruniversityService getUseruniversityService() {
		return useruniversityService;
	}

	public void setUseruniversityService(
			IUseruniversityService useruniversityService) {
		this.useruniversityService = useruniversityService;
	}
}
